package com.example.df.util;

import com.example.df.Quartz.QuartzService;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @author xu
 * @Description
 * @createTime 2021年03月09日 14:14:00
 */
@Component
public class QuartzreflectUtil {

    @SuppressWarnings("all")
    @Autowired
    QuartzService quartzService;

    static Logger logger = LoggerFactory.getLogger(QuartzreflectUtil.class);

    /**
     * 定时任务执行的方法，通过反射调用该类中的方法
     * @param context
     */
    @SuppressWarnings("all")
    public void reflect(JobExecutionContext context){
        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();

        String method = jobDataMap.getString("method");
        String cla = jobDataMap.getString("class");
        String jobName = jobDataMap.get("jobName").toString();
        String jobGroupName = jobDataMap.get("jobGroupName").toString();
        Object[] finalMethodParam = (Object[]) jobDataMap.get("finalMethodParam");
        //以上从controller接口接受参数
        Class aClass = null;                    //定义一个类
        try {
            aClass = Class.forName(cla);            //通过反射得到当前类
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        Method[] methods = aClass.getMethods();         //得到当前类中所有方法
        for (Method method1 : methods) {                //从方法数组中获取所有方法

            if (method1.getName().equals(method)) {              //判断方法名是否相等，当相等时会执行当前方法

                Class<?>[] parameterTypes = method1.getParameterTypes();        //获取当前方法的所有参数类型

                Method method2;
                try {
                    method2 = aClass.getMethod(method, parameterTypes);              //通过方法名和参数类型得到具体的方法
                    method2.invoke(aClass.getDeclaredConstructor().newInstance(),finalMethodParam);
                    System.out.println(LocalDateTime.now()+"\t\tJob运行");
                }catch (Exception e){
                    quartzService.deleteJob(jobName, jobGroupName);
                    logger.error("定时任务方法调用异常，已停止任务   jobName："+jobName+";\tjobGroupName:"+jobGroupName);
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 调用定时任务时，需要封装成obj[]才可以使用，工具类整合数据为obj[]
     * @param list          传递过来的list对象数据
     * @param obj               一个限定长度的obj[]对象
     * @param parameterCount            参数长度
     * @return
     */
    @SuppressWarnings("all")
    public static Object[] addObjelement(List list,Object[] obj,int parameterCount){
        if (list==null){
            return null;
        }
        for (int i = 0; i < parameterCount; i++) {                      //通过遍历，往obj2对象数组赋值
            String listClass = list.get(i).getClass().toString();           //得到list当前值的类型

            if (listClass.equals(HashMap.class.toString())) {               //判断类型是否相等。类型相等时，强转类型后复制obj
                obj[i] = (HashMap) list.get(i);
                logger.info("map赋值成功" + i);

            } else if (listClass.equals(String.class.toString())) {
                obj[i] = (String) list.get(i);
                logger.info("String赋值成功" + i);

            } else if (listClass.equals(Integer.class.toString())) {
                obj[i] = (Integer) list.get(i);
                logger.info("Integer赋值成功" + i);
            } else if (listClass.equals(Long.class.toString())) {
                obj[i] = (Long) list.get(i);
                logger.info("Long赋值成功" + i);
            }else if (listClass.equals(HttpServletRequest.class.toString())) {
                obj[i] = (HttpServletRequest) list.get(i);
                logger.info("HttpServletRequest赋值成功" + i);
            }else if (listClass.equals(HttpServletResponse.class.toString())) {
                obj[i] = (HttpServletResponse) list.get(i);
                logger.info("HttpServletResponse赋值成功" + i);
            }else if (listClass.equals(Map.class.toString())) {
                obj[i] = (Map) list.get(i);
                logger.info("Map赋值成功" + i);
            } else if (listClass.equals(LinkedHashMap.class.toString())) {
                obj[i] = (LinkedHashMap) list.get(i);
                logger.info("Map赋值成功" + i);
            }

        }
        return obj;
    }

    /**
     * 传入参数解析
     * @param paramSize         参数长度
     * @param param         map参数
     * @param response          响应对象
     * @param request           请求对象
     * @return          返回的需要调用定时器
     */
    @SuppressWarnings("all")
    public static List inputParamList(Integer paramSize,Map param,HttpServletResponse response,HttpServletRequest request){
        List<Object> list = new ArrayList();
        Map o;
        if (paramSize==0){
            return null;
        }
        for (int i = 1; i <= paramSize; i++) {
            o=(Map) param.get(i+"");
            if (o.containsKey("String")){
                String string = o.get("String").toString();
                list.add(string);
            }else if (o.containsKey("Map")){
                Map map = (Map) o.get("Map");
                list.add(map);
            } else if (o.containsKey("HttpServletResponse")){
                list.add(response);
            }else if (o.containsKey("HttpServletRequest")){
                list.add(request);
            }else if (o.containsKey("Long")){
                Long aLong = Long.valueOf(o.get("Long").toString());
                list.add(aLong);
            }else if (o.containsKey("Integer")){
                Integer integer = Integer.valueOf(o.get("Integer").toString());
                list.add(integer);
            }

        }
        return list;
    }

    /**
     * 传入参数解析
     * @param paramSize         参数长度
     * @param param         map参数
     * @param response          响应对象
     * @param request           请求对象
     * @return          返回的需要调用定时器
     */
    @SuppressWarnings("all")
    public static List inputParamListOptimize(Map param,HttpServletResponse response,HttpServletRequest request){
        List<Object> list = new ArrayList();
        List data=(List) param.get("paramdata");;
        if (data==null && data.size()==0){
            return null;
        }
        for (int i = 0; i < data.size(); i++) {
            Map o = (Map) data.get(i);
            if (o.containsKey("String")){
                String string = o.get("String").toString();
                list.add(string);
            }else if (o.containsKey("Map")){
                Map map = (Map) o.get("Map");
                list.add(map);
            } else if (o.containsKey("HttpServletResponse")){
                list.add(response);
            }else if (o.containsKey("HttpServletRequest")){
                list.add(request);
            }else if (o.containsKey("Long")){
                Long aLong = Long.valueOf(o.get("Long").toString());
                list.add(aLong);
            }else if (o.containsKey("Integer")){
                Integer integer = Integer.valueOf(o.get("Integer").toString());
                list.add(integer);
            }
        }
        return list;
    }
}

